<?php
Bxd::getInstance()->lib("validator")->loadClass("abstract/Code");
Bxd::getInstance()->lib("validator")->loadClass("interface/Check");

/**
 * The MIT License
 *
 * Copyright (c) <year> <copyright holders>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 *
 * @author Damiano Ciarla
 * @package lib
 * @name Email
 * @subpackage check
 */
class BXDValidatorCheckDate extends BXDValidatorCode implements BXDValidatorCheck
{
	public function __construct($value = null)
	{
		$this->_value = $value;
		$this->params('yyyy/mm/dd');
	}

	/**
	 * Validazione della regola
	 *
	 * @return boolean
	 */
	public function validate()
	{
		$yearFormat = "(19|20)?[0-9]{2}";

        switch($this->_params[0])
		{
            case 'dd/mm/yy':
            case 'dd-mm-yy':
                $format = "/^\b(0?[1-9]|[12][0-9]|3[01])[- \/.](0?[1-9]|1[012])[- \/.]{$yearFormat}\b$/";
                break;

            case 'mm/dd/yy':
            case 'mm-dd-yy':
                $format = "/^\b(0?[1-9]|1[012])[- \/.](0?[1-9]|[12][0-9]|3[01])[- \/.]{$yearFormat}\b$/";
                break;

            case 'mm/dd/yyyy':
            case 'mm-dd-yyyy':
                $format = "/^(0[1-9]|1[012])[- \/.](0[1-9]|[12][0-9]|3[01])[- \/.]{$yearFormat}$/";
                break;

            case 'dd/mm/yyyy':
            case 'dd-mm-yyyy':
                $format = "/^(0[1-9]|[12][0-9]|3[01])[- \/.](0[1-9]|1[012])[- \/.]{$yearFormat}$/";
                break;

            case 'yy/mm/dd':
            case 'yy-mm-dd':
                $format = "/^\b{$yearFormat}[- \/.](0?[1-9]|1[012])[- \/.](0?[1-9]|[12][0-9]|3[01])\b$/";
                break;

            case 'yyyy/mm/dd':
            case 'yyyy-mm-dd':
            default:
                $format = "/^\b{$yearFormat}[- \/.](0?[1-9]|1[012])[- \/.](0?[1-9]|[12][0-9]|3[01])\b$/";
                break;
        }

        if (!preg_match($format, $this->_value))
			return false;
		return true;
	}

	/**
	 * Messaggio di errore
	 *
	 * @return string
	 */
	public function message()
	{
		return sprintf("The value '%s' is not a valid date for the format '%s'", $this->_value, $this->_params[0]);
	}

	/**
	 * Codice dell'errore
	 *
	 * @return string
	 */
	public function code()
	{
		return self::$checkDateTime;
	}
}
?>
